var flag = false;//flag为false的时候，不能拖动div，flag为true的时候才能拖动div文件，
  
// 弹出层方法
function popupDiv(divId,popInfo,obj){
  var sourceDiv = "";
  $("#couidInput").val(obj.name);
  if(divId.tagName != undefined){
     sourceDiv = $(divId);
  }else{
    sourceDiv = $("#" + divId);
  }
  var title = sourceDiv.attr("title");
  if(title){
     $("#titleSpan").html(title);
  }
  $("#contentDiv").html(sourceDiv.html());
  var width = popInfo.width;
  var height = popInfo.height;
  if(width){
    $("#popupDiv").css("width",width);    
  }
  if(height){
    $("#popupDiv").css("height",height);
  }
  $("#divFirstCover").css("display","block");
  $("#popupDiv").css("display","block");
  init();
}
  
// 初始化
function init(){
  resetCoverDivSize();
  resetPopupDiv();
  swichImage(document.getElementById("closeImg"),"popupDiv_g_003.gif");
  colsePopupDiv();
  initTitleMove();
};
  
// 重新设置遮盖层属性
function resetCoverDivSize(){
  var coverDivHeight = document.body.scrollHeight ? document.body.scrollHeight : document.documentElement.scrollHeight;
  var coverDivWidth = document.body.scrollWidth ? document.body.scrollWidth : document.documentElement.scrollWidth;
  $("#divFirstCover").css("height",coverDivHeight);
  $("#divFirstCover").css("width",coverDivWidth);
  $("#divFirstCover").css("filter","alpha(opacity=45)");
}
  
// 重新设置弹出层的位置
function resetPopupDiv(){
  var popupDiv = $("#popupDiv").get(0);
  var width = (popupDiv.getBoundingClientRect().right - popupDiv.getBoundingClientRect().left);
  var height =(popupDiv.getBoundingClientRect().bottom - popupDiv.getBoundingClientRect().top);
  
  var left = (screen.availWidth-width) / 2;
  var top = (screen.availHeight - height)/ 2 - 100 + getScrollTop();
  $("#popupDiv").css("left",left);
  $("#popupDiv").css("top",top);
  
}
  
// 改变关闭按钮图片
function swichImage(curObj,newImg){
  var src = $(curObj).attr("src");
  var srcPath = src.substr(0,src.lastIndexOf("/") + 1);
  var newSrc = srcPath + newImg;
  $(curObj).mouseover(function(){
     $(this).attr("src",newSrc);
  });
  $(curObj).mouseout(function(){
     $(this).attr("src",src);
  });
  
}
  
 // 获取滚动条距离顶部距离
function getScrollTop() { 
	var yScroll; 
	if (self.pageYOffset) { 
	yScroll = self.pageYOffset; 
	        //xScroll = self.pageXOffset; 
	} else if (document.documentElement && document.documentElement.scrollTop){ 
	yScroll = document.documentElement.scrollTop; 
	} else if (document.body) { 
	yScroll = document.body.scrollTop; 
	} 
	return yScroll; 
}
  
// 点击关闭按钮关闭弹出层
function colsePopupDiv(){
  $("#closeImg").click(function(){
    $("#popupDiv").fadeOut("3000");
    $("#divFirstCover").fadeOut("3000");
  });
}

// 隐藏弹出层
function hidePopupDiv(){
	$("#popupDiv").fadeOut("3000");
    $("#divFirstCover").fadeOut("3000");
}
  
// 初始化标题栏目的移动方法
function initTitleMove(){
  $("#titleDiv").mouseout(function(){
    flag = false;
    $("#titleDiv").css("cursor","default");
  });
  $("#titleDiv").mouseup(function(){
    flag = false;
    $("#titleDiv").css("cursor","default");
  });
  $("#titleDiv").mousedown(function(){
    flag = true;
    $("#titleDiv").css("cursor","move");
  });
  $("#titleDiv").mousemove(function(){
    $("#titleDiv").css("cursor","move");
  });
}
	
// 当鼠标弹起的时候调用此方法
function mouseUp(divObj){
      flag = false;
      divObj.releaseCapture();
}

// 移动弹出层	
$(document).ready(function(){  
     var divObj = $("#popupDiv").get(0);
     var titleObj = $("#titleDiv").get(0);
     $(titleObj).mousedown(function(e){//e鼠标事件   
         if(flag){
           divObj.setCapture();
           $("#titleDiv").css("cursor","move");//改变鼠标指针的形状   
         }else{
           $(divObj).css("cursor","default");  
         }
         var offset = $(this).offset();//DIV在页面的位置   
         var x = e.pageX - offset.left;//获得鼠标指针离DIV元素左边界的距离   
         var y = e.pageY - offset.top;//获得鼠标指针离DIV元素上边界的距离   
         $(document).bind("mousemove",function(ev){//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件   
             $(divObj).stop();//加上这个之后   
             var _x = ev.pageX - x;//获得X轴方向移动的值   
             var _y = ev.pageY - y;//获得Y轴方向移动的值   
             $(divObj).animate({left:_x+"px",top:_y+"px"},10); 
         });  
     });  
     $(document).mouseup(function(){  
         $(divObj).css("cursor","default");  
         $(this).unbind("mousemove");  
         divObj.releaseCapture();
     })  
}) 